package culaocham.sinhvat.action;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import model.bean.DongVatModel;
import model.bean.ThucVatModel;
import model.bo.ProcessBO;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

public class GetDataAction extends Action{
	String name;
	ProcessBO proessBO;

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception { 
		name = new String(request.getParameter("name").getBytes("ISO8859_1"), "UTF8");
		proessBO = new ProcessBO();
		
		JSONObject json = new JSONObject();
		// get thong tin thuc vat de xu ly tren map
		if("thucvat".equals(name)){
			ArrayList<ThucVatModel> listThucVatModel = proessBO.listPoinThucVat();
			
			int i;
			for (i = 0; i < listThucVatModel.size(); i++) {
				JSONArray jsonArr = new JSONArray();
				jsonArr.put(listThucVatModel.get(i).getTv_loai());
				jsonArr.put(listThucVatModel.get(i).getTv_ho());
				jsonArr.put(listThucVatModel.get(i).getTv_chieuCao());
				jsonArr.put(listThucVatModel.get(i).getTv_duongKinh());
				jsonArr.put(listThucVatModel.get(i).getTv_doTanChe());
				jsonArr.put(listThucVatModel.get(i).getTv_loaiSinhTruong());
				jsonArr.put(listThucVatModel.get(i).getTv_giaTri());
				jsonArr.put(listThucVatModel.get(i).getTv_dangSong());
				jsonArr.put(listThucVatModel.get(i).getTv_dacDiem());
				jsonArr.put(listThucVatModel.get(i).getTv_viTri_long());
				jsonArr.put(listThucVatModel.get(i).getTv_viTri_lat());
				jsonArr.put(listThucVatModel.get(i).getTv_id());
				jsonArr.put(listThucVatModel.get(i).getTv_image());
				json.put(String.valueOf(listThucVatModel.get(i).getTv_stt()),jsonArr);
			}
			json.put("length", i); // them so luong cua list json
			
			// get thong tin dong vat de xu ly tren map
		} else if("dongvat".equals(name)){
			ArrayList<DongVatModel> listDongVatModel = proessBO.listPoinDongVat();

			int i;
			for (i = 0; i < listDongVatModel.size(); i++) {
				JSONArray jsonArr = new JSONArray();
				jsonArr.put(listDongVatModel.get(i).getDv_loai());
				jsonArr.put(listDongVatModel.get(i).getDv_ho());
				jsonArr.put(listDongVatModel.get(i).getDv_gioi());
				jsonArr.put(listDongVatModel.get(i).getDv_nganh());
				jsonArr.put(listDongVatModel.get(i).getDv_lop());
				jsonArr.put(listDongVatModel.get(i).getDv_bo());
				jsonArr.put(listDongVatModel.get(i).getDv_chi());
				jsonArr.put(listDongVatModel.get(i).getDv_noiSong());
				jsonArr.put(listDongVatModel.get(i).getDv_dacDiem());
				jsonArr.put(listDongVatModel.get(i).getDv_thucAn());
				jsonArr.put(listDongVatModel.get(i).getDv_viTri_long());
				jsonArr.put(listDongVatModel.get(i).getDv_viTri_lat());
				jsonArr.put(listDongVatModel.get(i).getDv_id());
				jsonArr.put(listDongVatModel.get(i).getDv_image());
				json.put(String.valueOf(listDongVatModel.get(i).getDv_stt()),jsonArr);
			}
			json.put("length", i); // them so luong cua list json
			
		// get thong tin cua tat ca thuc vat de set vao datatable thuc vat
		} else if("datatableThucVat".equals(name)){
			ArrayList<ThucVatModel> listThucVatDatatable = proessBO.listInfoThucVat();
			JSONArray jsonDataTableTV = new JSONArray();
			
			for (int i = 0; i < listThucVatDatatable.size(); i++) {
				JSONArray jsonArr = new JSONArray();
				jsonArr.put(listThucVatDatatable.get(i).getTv_stt());
				jsonArr.put(listThucVatDatatable.get(i).getTv_loai());
				jsonArr.put(listThucVatDatatable.get(i).getTv_ho());
				jsonArr.put(listThucVatDatatable.get(i).getTv_chieuCao());
				jsonArr.put(listThucVatDatatable.get(i).getTv_duongKinh());
				jsonArr.put(listThucVatDatatable.get(i).getTv_doTanChe());
				jsonArr.put(listThucVatDatatable.get(i).getTv_loaiSinhTruong());
				jsonArr.put(listThucVatDatatable.get(i).getTv_giaTri());
				jsonArr.put(listThucVatDatatable.get(i).getTv_dangSong());
				jsonArr.put(listThucVatDatatable.get(i).getTv_dacDiem());
				jsonArr.put(listThucVatDatatable.get(i).getTv_soLuong());
				jsonDataTableTV.put(jsonArr);
			}
			json.put("datatableThucVat", jsonDataTableTV);
			
			// get thong tin cua tat ca dong vat de set vao datatable dong vat
		} else if("datatableDongVat".equals(name)){
			ArrayList<DongVatModel> listDongVatDatatable = proessBO.listInfoDongVat();
			JSONArray jsonDataTableDV = new JSONArray();
			
			for (int i = 0; i < listDongVatDatatable.size(); i++) {
				JSONArray jsonArr = new JSONArray();
				jsonArr.put(listDongVatDatatable.get(i).getDv_stt());
				jsonArr.put(listDongVatDatatable.get(i).getDv_loai());
				jsonArr.put(listDongVatDatatable.get(i).getDv_ho());
				jsonArr.put(listDongVatDatatable.get(i).getDv_gioi());
				jsonArr.put(listDongVatDatatable.get(i).getDv_nganh());
				jsonArr.put(listDongVatDatatable.get(i).getDv_lop());
				jsonArr.put(listDongVatDatatable.get(i).getDv_bo());
				jsonArr.put(listDongVatDatatable.get(i).getDv_chi());
				jsonArr.put(listDongVatDatatable.get(i).getDv_noiSong());
				jsonArr.put(listDongVatDatatable.get(i).getDv_thucAn());
				jsonArr.put(listDongVatDatatable.get(i).getDv_dacDiem());
				jsonArr.put(listDongVatDatatable.get(i).getDv_soLuong());
				jsonDataTableDV.put(jsonArr);
			}
			json.put("datatableDongVat", jsonDataTableDV);
			
		// get thong tin thuc vat tu nhung thuc vat da duoc select de set vao datatable thuc vat
		} else if("datatableSelectTV".equals(name)){
			String strSelectTV = new String(request.getParameter("arraySelectTV").getBytes("ISO8859_1"), "UTF8");
			ArrayList<String> seleteLoaiTV = new ArrayList<String>();
			int index = 0;
			int kt = 1;
			for(int i = 0; i< strSelectTV.length(); i++){
				while(i < strSelectTV.length() && strSelectTV.charAt(i) != '"') i++;
				if(kt % 2 == 0){
					seleteLoaiTV.add(strSelectTV.substring(index + 1, i));
				}
				kt++;
				index = i;
			}
			ArrayList<ThucVatModel> listThucVatDatatable = proessBO.listInfoThucVat();
			JSONArray jsonDataTableTV = new JSONArray();
			
			for (int i = 0; i < listThucVatDatatable.size(); i++) {
				for (int j = 0; j<seleteLoaiTV.size(); j++){
					if(seleteLoaiTV.get(j).equals(listThucVatDatatable.get(i).getTv_loai())){
						JSONArray jsonArr = new JSONArray();
						jsonArr.put(listThucVatDatatable.get(i).getTv_stt());
						jsonArr.put(listThucVatDatatable.get(i).getTv_loai());
						jsonArr.put(listThucVatDatatable.get(i).getTv_ho());
						jsonArr.put(listThucVatDatatable.get(i).getTv_chieuCao());
						jsonArr.put(listThucVatDatatable.get(i).getTv_duongKinh());
						jsonArr.put(listThucVatDatatable.get(i).getTv_doTanChe());
						jsonArr.put(listThucVatDatatable.get(i).getTv_loaiSinhTruong());
						jsonArr.put(listThucVatDatatable.get(i).getTv_giaTri());
						jsonArr.put(listThucVatDatatable.get(i).getTv_dangSong());
						jsonArr.put(listThucVatDatatable.get(i).getTv_dacDiem());
						jsonArr.put(listThucVatDatatable.get(i).getTv_soLuong());
						jsonDataTableTV.put(jsonArr);
						j = seleteLoaiTV.size() - 1;  // break khoi vong lap for
					}
				}
			}
			json.put("datatableThucVat", jsonDataTableTV);
		
			// get thong tin dong vat tu nhung dong vat da duoc select de set vao datatable dong vat	
		}  else if("datatableSelectDV".equals(name)){
			String strSelectDV = new String(request.getParameter("arraySelectDV").getBytes("ISO8859_1"), "UTF8");
			ArrayList<String> seleteLoaiDV = new ArrayList<String>();
			int index = 0;
			int kt = 1;
			for(int i = 0; i< strSelectDV.length(); i++){
				while(i < strSelectDV.length() && strSelectDV.charAt(i) != '"') i++;
				if(kt % 2 == 0){
					seleteLoaiDV.add(strSelectDV.substring(index + 1, i));
				}
				kt++;
				index = i;
			}
			ArrayList<DongVatModel> listDongVatDatatable = proessBO.listInfoDongVat();
			JSONArray jsonDataTableDV = new JSONArray();
			
			for (int i = 0; i < listDongVatDatatable.size(); i++) {
				for (int j = 0; j<seleteLoaiDV.size(); j++){
					if(seleteLoaiDV.get(j).equals(listDongVatDatatable.get(i).getDv_loai())){
						JSONArray jsonArr = new JSONArray();
						jsonArr.put(listDongVatDatatable.get(i).getDv_stt());
						jsonArr.put(listDongVatDatatable.get(i).getDv_loai());
						jsonArr.put(listDongVatDatatable.get(i).getDv_ho());
						jsonArr.put(listDongVatDatatable.get(i).getDv_gioi());
						jsonArr.put(listDongVatDatatable.get(i).getDv_nganh());
						jsonArr.put(listDongVatDatatable.get(i).getDv_lop());
						jsonArr.put(listDongVatDatatable.get(i).getDv_bo());
						jsonArr.put(listDongVatDatatable.get(i).getDv_chi());
						jsonArr.put(listDongVatDatatable.get(i).getDv_noiSong());
						jsonArr.put(listDongVatDatatable.get(i).getDv_thucAn());
						jsonArr.put(listDongVatDatatable.get(i).getDv_dacDiem());
						jsonArr.put(listDongVatDatatable.get(i).getDv_soLuong());
						jsonDataTableDV.put(jsonArr);
						j = seleteLoaiDV.size() - 1;  // break khoi vong lap for
					}
				}
			}
			json.put("datatableDongVat", jsonDataTableDV);
		} else if("thongKeChung".equals(name)){
			JSONObject jsonPoint = new JSONObject(request.getParameter("arrayChoicePolygonTV")); //lay mang point tu clent tra ve
			JSONArray arrJsonPoint = new JSONArray();  //khai bao mang luu cac point
			JSONObject point = new JSONObject();  // khai bao bien luu mot point
			JSONArray jsonArr; // khai bao bien luu thong tin cua tung ca the dong vat, thuc vat
			
			arrJsonPoint = (JSONArray) jsonPoint.get("b");
			double arrDoublePoint[][] = new double[arrJsonPoint.length()][2]; // khai bao mang point chuyen sang double
			for(int k = 0; k < arrJsonPoint.length(); k++){
				point = (JSONObject) arrJsonPoint.get(k);
				arrDoublePoint[k][0] = point.getDouble("jb");
				arrDoublePoint[k][1] = point.getDouble("kb");
			}
			
			ArrayList<ThucVatModel> listThucVatInfo = proessBO.listInfoThucVat();
			// lay cac thuc vat nam trong vung da chon
			ArrayList<ThucVatModel> listThucVatDatatable = proessBO.listPoinThucVat();
			JSONArray jsonDataTableTV = new JSONArray(); // khoi tao bien JSONArray luu ket qua tra ve cho client
			
			int p = 0, i, j, n = arrDoublePoint.length;
			int s, kt;
			ArrayList<Integer> arrLoai = new ArrayList<Integer>();
			while(p < listThucVatDatatable.size()){
				kt = 0;
				for(i = 0; i < n; i++){
					j = (i+1)%n;
					s = checkInOut(arrDoublePoint[i][0], arrDoublePoint[i][1], arrDoublePoint[j][0], arrDoublePoint[j][1], listThucVatDatatable.get(p).getTv_viTri_lat(), listThucVatDatatable.get(p).getTv_viTri_long());
					for(int k = 0; k < n; k++){
						if((k != i) && (k != j))
							if(s != checkInOut(arrDoublePoint[i][0], arrDoublePoint[i][1], arrDoublePoint[j][0], arrDoublePoint[j][1], arrDoublePoint[k][0], arrDoublePoint[k][1])){
								i = n - 1;
								kt = 1;
							}
					}
				}
				if(kt == 0) arrLoai.add(p); 
				p++;
			}
			int m = 0, l, sumLoai, stt = 1;
			while(m < arrLoai.size()){
				l = 0;
				sumLoai = 0;
				while((l < m) && !(listThucVatDatatable.get(arrLoai.get(l)).getTv_loai().equals(listThucVatDatatable.get(arrLoai.get(m)).getTv_loai()))) l++;
				if(l == m){
					for(i = m; i< arrLoai.size(); i++){
						if(listThucVatDatatable.get(arrLoai.get(m)).getTv_loai().equals(listThucVatDatatable.get(arrLoai.get(i)).getTv_loai())) sumLoai++;
					}
					jsonArr = new JSONArray();
					jsonArr.put(stt);
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_loai());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_ho());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_chieuCao());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_duongKinh());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_doTanChe());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_loaiSinhTruong());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_giaTri());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_dangSong());
					jsonArr.put(listThucVatDatatable.get(arrLoai.get(m)).getTv_dacDiem());
					int x = 0;
					while (x < listThucVatInfo.size() && !(listThucVatInfo.get(x).getTv_loai().equals(listThucVatDatatable.get(arrLoai.get(m)).getTv_loai()))) x++;
					jsonArr.put(sumLoai + "/" + listThucVatInfo.get(x).getTv_soLuong());
					jsonDataTableTV.put(jsonArr);
					
					stt++;
				}
				m++;
			}
			json.put("datatableThucVat", jsonDataTableTV);
			
			ArrayList<DongVatModel> listDongVatInfo = proessBO.listInfoDongVat();
			// lay cac dong vat nam trong vung da chon
			ArrayList<DongVatModel> listDongVatDatatable = proessBO.listPoinDongVat();
			JSONArray jsonDataTableDV = new JSONArray(); // khoi tao bien JSONArray luu ket qua tra ve cho client
			
			n = arrDoublePoint.length;
			arrLoai.clear();
			p = 0;
			while(p < listDongVatDatatable.size()){
				kt = 0;
				for(i = 0; i < n; i++){
					j = (i+1)%n;
					s = checkInOut(arrDoublePoint[i][0], arrDoublePoint[i][1], arrDoublePoint[j][0], arrDoublePoint[j][1], listDongVatDatatable.get(p).getDv_viTri_lat(), listDongVatDatatable.get(p).getDv_viTri_long());
					for(int k = 0; k < n; k++){
						if((k != i) && (k != j))
							if(s != checkInOut(arrDoublePoint[i][0], arrDoublePoint[i][1], arrDoublePoint[j][0], arrDoublePoint[j][1], arrDoublePoint[k][0], arrDoublePoint[k][1])){
								i = n - 1;
								kt = 1;
							}
					}
				}
				if(kt == 0) arrLoai.add(p); 
				p++;
			}
			m = 0;
			sumLoai = 0;
			stt = 1;
			while(m < arrLoai.size()){
				l = 0;
				sumLoai = 0;
				while((l < m) && !(listDongVatDatatable.get(arrLoai.get(l)).getDv_loai().equals(listDongVatDatatable.get(arrLoai.get(m)).getDv_loai()))) l++;
				if(l == m){
					for(i = m; i< arrLoai.size(); i++){
						if(listDongVatDatatable.get(arrLoai.get(m)).getDv_loai().equals(listDongVatDatatable.get(arrLoai.get(i)).getDv_loai())) sumLoai++;
					}
					jsonArr = new JSONArray();
					jsonArr.put(stt);
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_loai());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_ho());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_gioi());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_nganh());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_lop());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_bo());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_chi());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_noiSong());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_thucAn());
					jsonArr.put(listDongVatDatatable.get(arrLoai.get(m)).getDv_dacDiem());
					int x = 0;
					while (x < listDongVatInfo.size() && !(listDongVatInfo.get(x).getDv_loai().equals(listDongVatDatatable.get(arrLoai.get(m)).getDv_loai()))) x++;
					jsonArr.put(sumLoai + "/" + listDongVatInfo.get(x).getDv_soLuong());
					jsonDataTableDV.put(jsonArr);
					
					stt++;
				}
				m++;
			}
			json.put("datatableDongVat", jsonDataTableDV);
		}
		
		response.getWriter().write(json.toString());
 
		return null;
	}
	
	private int checkInOut(Double Ax, Double Ay, Double Bx, Double By, Double Cx, Double Cy) {
		double kt = (Cx - Ax)*(By - Ay) - (Cy - Ay)*(Bx - Ax);
		return kt<0?-1:1;
	}
}
